package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.*;
import com.itheima.pojo.*;
import com.itheima.service.CheckGroupYYService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

@Service(interfaceClass = CheckGroupYYService.class)
@Transactional
public class CheckGroupYYServiceImpl implements CheckGroupYYService {

    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    CheckGroupYYMapper checkGroupYYMapper;
    @Override
    public Result addYY(OrderInfoVo orderInfoVo) {
        Date orderDate = orderInfoVo.getOrderDate();
        String telephone = orderInfoVo.getTelephone();
        String name = orderInfoVo.getName();
        Integer sex = orderInfoVo.getSex();
        String idCard = orderInfoVo.getIdCard();
        Integer setmealId = orderInfoVo.getSetmealId();
        String orderType = orderInfoVo.getOrderType();
        List<Integer> setmealIds = orderInfoVo.getSetmealIds();

        //1.根据预约日期查询是否存在预约设置
        OrderSetting orderSetting = orderSettingMapper.findByDate(orderDate);
        if (null == orderSetting) {
            return Result.error("没有档期");
        }
        //2.查看是否约满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (number <= reservations) {
            return Result.error("预约已满");
        }
        //3.根据手机号码查询是否是我们的会员
        Member member = memberMapper.findByTelephone(telephone);
        //4.如果不是就自动注册
        if (null==member){
           member = new Member();
            member.setName(name);
            member.setSex(String.valueOf(sex));
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberMapper.add(member);//一定要返回主键
        }

        Integer memberId = member.getId();
        //5.根据会员id、套餐id、预约日期是否已经存在预约信息
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        for (Integer i:setmealIds){
            order.setSetmealId(i);
            List<Order> orders = orderMapper.findByCondition(order);
            if (CollectionUtil.isNotEmpty(orders)) {
                return Result.error("不要重复预约");
            }
        }


        //6.直接下单
        for (Integer i:setmealIds){
            order.setSetmealId(i);
            checkGroupYYMapper.addYY(order);

        }

        //7.已预约人数加+1
        orderSetting.setReservations(reservations + 1);
        orderSettingMapper.editYY(orderSetting);

        return Result.success("添加成功");//页面需要使用订单id
    }

    @Override
    public PageResult findPages(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<OrderInfoVo> orderInfoVos = setmealMapper.findPages(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), orderInfoVos);

    }

    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> setmeals = setmealMapper.findAll();
        return setmeals;
    }

    @Override
    public void delete(OrderInfoVo orderInfoVo) {
       orderMapper.delete(orderInfoVo);
       Integer i=orderMapper.findByMemberIdAndSetmealId(orderInfoVo);
       if (i==0){
           Date orderDate = orderInfoVo.getOrderDate();
           OrderSetting orderSetting = orderSettingMapper.findByDate(orderDate);
           int reservations = orderSetting.getReservations();
           orderSetting.setReservations(reservations-1);
           orderSettingMapper.editYY(orderSetting);
       }
    }


}
